var app = getApp();
import { api, util, dialog } from '../../../../js/util'
import { retreat } from "../../../../js/modules/order.js"
const mall = require("../../../../js/modules/mall.js");

import authorize from '../../../../js/modules/authorize.js'
import BasePage from '../../../../js/common/base-page'
BasePage({
  pageKey:"retreatList",

  /**
   * 页面的初始数据
   */
  data: {
    imgDomain: api.imgDomain,
    enableOss: api.enableOss,
    tablist: ['全部', '待审核', '已审核', '待收货', '待发货/退款', '已发货/退款', '已完成', '审核失败', '已取消'],
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 10,
    slidewidth: 'auto',
    lists: [{
      activeIndex: 0,
      status: -30,//全部
      list: [],
      hasMoreData: true,
      isLoadComplete: false,
      page: {
        PageIndex: 1,
        PageSize: 20,
        keywords: "",
        sortField: "",
        sortDirect: "desc"
      }
    }, {
      activeIndex: 1,
        status: 0,//待审核
      list: [],
      hasMoreData: true,
      isLoadComplete: false,
      page: {
        PageIndex: 1,
        PageSize: 20,
        keywords: "",
        sortField: "",
        sortDirect: "desc"
      }
    }, {
      activeIndex: 2,
        status: 10,//已审核
      list: [],
      hasMoreData: true,
      isLoadComplete: false,
      page: {
        PageIndex: 1,
        PageSize: 20,
        keywords: "",
        sortField: "",
        sortDirect: "desc"
      }
    }, {
      activeIndex: 3,
        status: 20,//待收货
      list: [],
      hasMoreData: true,
      isLoadComplete: false,
      page: {
        PageIndex: 1,
        PageSize: 20,
        keywords: "",
        sortField: "",
        sortDirect: "desc"
      }
    }, {
      activeIndex: 4,
        status: 30,//待发货/退款
      list: [],
      hasMoreData: true,
      isLoadComplete: false,
      page: {
        PageIndex: 1,
        PageSize: 20,
        keywords: "",
        sortField: "",
        sortDirect: "desc"
      }
    },
      {
        activeIndex: 5,
        status: 40,//已发货/退款
        list: [],
        hasMoreData: true,
        isLoadComplete: false,
        page: {
          PageIndex: 1,
          PageSize: 20,
          keywords: "",
          sortField: "",
          sortDirect: "desc"
        }
      },
      {
        activeIndex: 6,
        status: 50,//已完成
        list: [],
        hasMoreData: true,
        isLoadComplete: false,
        page: {
          PageIndex: 1,
          PageSize: 20,
          keywords: "",
          sortField: "",
          sortDirect: "desc"
        }
      },
      {
        activeIndex: 7,
        status: -10,//审核失败
        list: [],
        hasMoreData: true,
        isLoadComplete: false,
        page: {
          PageIndex: 1,
          PageSize: 20,
          keywords: "",
          sortField: "",
          sortDirect: "desc"
        }
      }
      , {
        activeIndex: 8,
        status: -20,//已取消
        list: [],
        hasMoreData: true,
        isLoadComplete: false,
        page: {
          PageIndex: 1,
          PageSize: 20,
          keywords: "",
          sortField: "",
          sortDirect: "desc"
        }
      }
    ]

  },

  /**
  * 生命周期函数--监听页面加载
  */
  onLoad: function (options) {

  },
  /**加载商品列表 */
  loadList() {
    var that = this;
    var current = this.data.lists[that.data.activeIndex];
    var param = current.page;
    retreat.loadList(current.status, param, function (content, fkid) {
      var hasMoreData = content.count > (current.page.PageIndex) * current.page.PageSize;
      var list_key = "lists[" + that.data.activeIndex + "].list";
      var list_has = "lists[" + that.data.activeIndex + "].hasMoreData";
      let isLoadComplete = "lists[" + that.data.activeIndex + "].isLoadComplete";
      var index = that.data.lists[that.data.activeIndex].list.length;
      var data = content.data;
      //当分页大于1时，累加
      if (current.page.PageIndex > 1) {
        data = that.data.lists[that.data.activeIndex].list.concat(data);
      }
      //如果key为变量作为key值的时候要用[]包裹起来
      that.setData({
        [list_key]: data,
        [list_has]: hasMoreData,
        [isLoadComplete]: true
      });
    })
  },
  /**
   * 关键词查询
   */
  onSearchKeywordEvent: function (e) {
    // console.log(e.detail);
    this.cleanList(this);
    for (var i = 0; i < this.data.lists.length; i++) {
      this.data.lists[i].page.keywords = e.detail;
    }
    this.loadList();
  },
  /**
* 页面相关事件处理函数--监听用户下拉动作
*/
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    let {
      lists,
      activeIndex
    } = this.data
    let current = lists[activeIndex]
    current.page.PageIndex = 1
    current.isLoadComplete = false
    lists[activeIndex] = current
    this.setData({
      lists
    })
    this.loadList();
    // console.log("onPullDownRefresh");
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function (e) {
    // console.log("onReachBottom");
    var current = this.data.lists[this.data.activeIndex];
    if (current.hasMoreData) {
      var index = this.data.lists[this.data.activeIndex].page.PageIndex;
      // console.log(index + 1);
      this.data.lists[this.data.activeIndex].page.PageIndex = index + 1;
      this.loadList();
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.cleanList(this);
    this.loadList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
    * 切换当前选择项
    */
  onSwitchItem: function (res) {
    if (this.data.activeIndex == res.detail.currentNum.id) return;
    var activeIndex =parseInt(res.detail.currentNum.id);
    this.setData({
      sliderOffset: res.detail.currentNum.offsetLeft,
      activeIndex: activeIndex
    });
    var current = this.data.lists[activeIndex];
    if (current!=undefined&&(current.list == null || current.list.length == 0)) {
      this.loadList();
    }

  },
  /**
   * 清除每一列
   */
  cleanList: function (that) {
    for (var i = 0; i < that.data.lists.length; i++) {
      that.data.lists[i].list = [];
      that.data.lists[i].page.PageIndex = 1;
    }

  },
  onUnload: function (e) {
    this.data.pre_page = this.getPreData();
    this.data.pre_page.data.isBack = true;
  },
  getPreData: function () {
    const pages = getCurrentPages();
    const length = pages.length;
    const prePage = pages[length - 2]; //上一页
    return prePage;
  }
})